package weaver.batchchanges.wrapper;

import com.alibaba.nacos.client.utils.StringUtils;
import weaver.security.webcontainer.XssRequestWeblogic;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 任务批量变更自定义request类，用于给request添加自定义参数
 *
 * @author wujinghe
 * @since 2024-10-09
 */
public class BatchTasksChangesRequestWrapper extends XssRequestWeblogic {
    public BatchTasksChangesRequestWrapper(HttpServletRequest request, Map<String, Object> parameterMap) {
        super(request);
        this.parameterMap.putAll(parameterMap);
    }

    private final Map<String, Object> parameterMap = new HashMap();

    @Override
    public String getParameter(String arg0) {
        String parameter = super.getParameter(arg0);
        if (StringUtils.isEmpty(parameter)) {
            parameter = String.valueOf(parameterMap.get(arg0));
        }
        return parameter;
    }

    @Override
    public Map getParameterMap() {
        Map superParameterMap = super.getParameterMap();
        HashMap<String, Object> parameterMap = new HashMap<>();
        parameterMap.putAll(superParameterMap);
        parameterMap.putAll(this.parameterMap);
        return Collections.unmodifiableMap(parameterMap);
    }

    public static class Builder {
        private Map<String, Object> parameterMap = new HashMap<>();

        public Builder addParameter(String arg0, Object arg1) {
            parameterMap.put(arg0, arg1);
            return this;
        }

        public BatchTasksChangesRequestWrapper build(HttpServletRequest request) {
            return new BatchTasksChangesRequestWrapper(request, parameterMap);
        }
    }


    public void addParameter(String arg0, Object arg1) {
        parameterMap.put(arg0, arg1);
    }

    public void addAllParameter(Map<String, Object> arg0) {
        parameterMap.putAll(arg0);
    }
}
